package com.zxf.basic.base

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialog
import androidx.viewbinding.ViewBinding

/**
 * @author zjc
 * @date 2022/12/27
 */
open class BindingDialog <VB : ViewBinding>(private val ctx: Context, private val init: (inflater: LayoutInflater) -> VB) : AppCompatDialog(ctx)  {

    lateinit var binding: VB
    private set

    open var width = 0
    open var height = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = init.invoke(layoutInflater)
        setContentView(binding.root)
    }

    override fun onStart() {
        super.onStart()
        window?.setBackgroundDrawable(ColorDrawable(0))
        width = if (width == 0) (ctx.resources.displayMetrics.widthPixels * 0.75).toInt() else width
        height = if (height == 0) WindowManager.LayoutParams.WRAP_CONTENT else height
        window?.setLayout(width, height)
    }

}